const CHUNK_SIZE = 1024 * 1024 * 5; // 5MB 分片大小

const THREAD_COUNT = navigator.hardwareConcurrency || 4;

export function cutFile(file) {
    return new Promise((resolve) => {
        const chunkCount = Math.ceil(file.size / CHUNK_SIZE);
        const threadChunkCount = Math.ceil(chunkCount / THREAD_COUNT);
        const result = [];
        let finishCount = 0;
        for (let i = 0; i < THREAD_COUNT; i++) {
            const worker = new Worker('./js/worker.js', {
                type: 'module'
            });
            let start = i * threadChunkCount;
            let end = (i + 1) * threadChunkCount;
            if (i > chunkCount) {
                end = chunkCount;
            }
            worker.postMessage({
                file,
                CHUNK_SIZE,
                startChunkIndex: start,
                endChunkIndex: end
            });
            worker.onmessage = (e) => {
                for (let i = start; i < end; i++) {
                    result[i] = e.data[i - start];
                }
                worker.terminate();
                finishCount++;
                if (finishCount === THREAD_COUNT) {
                    console.log('finishCount === THREAD_COUNT', result);
                    resolve(result);
                }
            }
        }
    });
}